$(document).ready(function () {
	// 评论登录链接
	$('#linkLogin').click(function (event) {
		event.preventDefault();
		
		$('.modal-content').load('./login', function () {
				
				$('#loginForm').submit(function (event) {
					event.preventDefault();
					var formValues = $(this).serialize();
					$.post('/login', formValues, function (result) {
						if(result.status == 0) {
							swal({
							  title: "登录失败",
							  text: "账号或密码错误！",
							  type: "error"
							});
						}else if(result.status == 1) {
							swal({
							  title: "登录成功",
							  text: "欢迎进入 News新闻……",
							  timer: 600,
							  showConfirmButton: false
							});
							
							setTimeout(function(){
								var oldHref = window.location.href;
							    window.location.href = oldHref;
							}, 600);
						}
					});
				});
				
		});
		$('#modal').modal();
	});
	
	// 收藏功能
	$('button#collect').click(function () {
		var $this = $(this);
		var newsId = $('.news-header').attr('data-newId');
		var data = {
			"newsId": newsId
		};
		// 取消收藏
		if($this.has('span.glyphicon-star-empty').length > 0) {
			$.post('/users/uncollect', data, function (result) {
				if(result.status == 1) {
					$this.find('span.glyphicon-star-empty').remove();
				}else {
					// ……	
				}
			});
		// 收藏
		}else {
			$.post('/users/collect', data, function (result) {
				if(result.status == 1) {
					$this.prepend('<span class="glyphicon glyphicon-star-empty" aria-hidden="true"></span>');	
				}else {
					// ……	
				}
			});
		}
	});
	
	// 提交0级回复
	$('#commentForm').submit(function (event) {
		event.preventDefault();
		
		var $commentZero = $('#commentZero');
		var formValues = $commentZero.val();
		var newsId = $('.news-header').attr('data-newId');
		var data = {
			"formValues": formValues,
			"newsId": newsId
		};
		$.post('/news/comment', data, function (result) {
			if(result.status == 0) {
				swal({
				  title: "回复失败",
				  text: result.data.message,
				  type: "error"
				});
			}else if(result.status == 1) {
				swal({
				  title: "回复成功",
				  type: "success"
				}, function () {
					var html = '<li class="media" data-cid = ';
					html += result.data.commentId;
					html += ' data-id = ';
					html += result.data.id;
					html += '><div class="media-left"><a href="#"><img class="media-object img-circle" src="';
					html += result.data.userPicture;
					html += '" alt="..."></a></div>';
					html += '<div class="media-body"><h4 class="media-heading">';
					html += result.data.sendName;
					html += '</h4><p>';
					html += result.data.content;
					html += '</p><p class="text-right"><time class="span-info">';
					html += result.data.sendTime;
					html += '</time><a href="#" class="span-info" data-value="commentOneForm">回复</a></p></div><ul class="CommentContent1"></ul></li>';
					$(html).prependTo('ul.CommentContent0');
					
					$commentZero.val('');
				});
			}
		});
	});
	
	// 提交1级回复
	$('#commentContent').click(function (event) {
		var $eventDom = $(event.target);
		if($eventDom.is('#commentContent a[data-value="commentOneForm"]')) {
			event.preventDefault();
			
			// 显示一级回复框
			$('#commentContent form').remove();
			var $thisMediaBody = $eventDom.closest('div.media-body');
			var name = $thisMediaBody.find('h4.media-heading').text();
			var html = '<form id="commentOneForm"><div class="form-group"><label for="commentOne"><small>回复：';
			html += name;
			html += '</small></label><textarea  class="form-control" id="commentOne" placeholder="请输入评论……" rows="3"></textarea><input type="hidden" value="0" /></div><div class="text-right"><button type="submit" class="btn btn-primary">Submit</button></div></form>';
			$thisMediaBody.append(html);
			
			// 获取一级表单
			var $thisOneForm = $thisMediaBody.find('form#commentOneForm');
			$thisOneForm.submit(function (event) {
				event.preventDefault();
				
				var content = $thisOneForm.find('#commentOne').val();
				var data = {
					"content": content,
					"newsId": $('.news-header').attr('data-newId'),
					"beCommentId": $thisMediaBody.parent().attr('data-cid'),
					"mainCommentObjectId": $thisMediaBody.parent().attr('data-id'),
					"beCommentName": name
				};
				
				$.post('/news/commentOneTwo', data, function (result) {
					if(result.status == 0) {
						swal({
						  title: "回复失败",
						  text: result.data.message,
						  type: "error"
						});
					}else if(result.status == 1) {
						swal({
						  title: "回复成功",
						  type: "success"
						}, function () {
							var html = '<li class="media" data-cid=';
							html += result.data.commentId;
							html += '><div class="media-left"><a href="#"><img class="media-object img-circle" src="'
							html += result.data.userPicture;
							html += '" alt="..."></a></div>';
							html += '<div class="media-body"><h4 class="media-heading">';
							html += result.data.sendName;
							html += '&nbsp;&nbsp;<small>回复：';
							html += result.data.beCommentName;
							html += '</small></h4><p>';
							html += result.data.content;
							html += '</p><p class="text-right"><time class="span-info">';
							html += result.data.sendTime;
							html += '</time><a href="#" class="span-info" data-value="commentSecondForm">回复</a></p></div></li>';
							var $target = $thisMediaBody.parent().find('ul.CommentContent1');
							$target.append(html);
							
							$('#commentContent form').remove();
						});
					}
				});
			});
			
		// 提交2级评论
		}else if($eventDom.is('#commentContent a[data-value="commentSecondForm"]')) {
			event.preventDefault();
			
			// 显示二级回复框
			$('#commentContent form').remove();
			var $thisMediaBody = $eventDom.closest('div.media-body');
			var name = $thisMediaBody.find('h4.media-heading').attr('data-name');
			var html = '<form id="commentSecondForm"><div class="form-group"><label for="commentOne"><small>回复：';
			html += name;
			html += '</small></label><textarea  class="form-control" id="commentTwo" placeholder="请输入评论……" rows="3"></textarea><input type="hidden" value="0" /></div><div class="text-right"><button type="submit" class="btn btn-primary">Submit</button></div></form>';
			$thisMediaBody.append(html);
			
			// 获取二级表单
			var $thisSecondForm = $thisMediaBody.find('form#commentSecondForm');
			$thisSecondForm.submit(function (event) {
				event.preventDefault();
				
				var content = $thisSecondForm.find('#commentTwo').val();
				var data = {
					"content": content,
					"newsId": $('.news-header').attr('data-newId'),
					"beCommentId": $thisMediaBody.parent().attr('data-cid'),
					"mainCommentObjectId": $thisMediaBody.closest('ul.CommentContent1').parent().attr('data-id'),
					"beCommentName": name
				};
				
				$.post('/news/commentOneTwo', data, function (result) {
					if(result.status == 0) {
						swal({
						  title: "回复失败",
						  text: result.data.message,
						  type: "error"
						});
					}else if(result.status == 1) {
						swal({
						  title: "回复成功",
						  type: "success"
						}, function () {
							var html = '<li class="media" data-cid=';
							html += result.data.commentId;
							html += '><div class="media-left"><a href="#"><img class="media-object img-circle" src="'
							html += result.data.userPicture;
							html += '" alt="..."></a></div>';
							html += '<div class="media-body"><h4 class="media-heading" data-name=';
							html += result.data.sendName;
							html += '>';
							html += result.data.sendName;
							html += '&nbsp;&nbsp;<small>回复：';
							html += result.data.beCommentName;
							html += '</small></h4><p>';
							html += result.data.content;
							html += '</p><p class="text-right"><time class="span-info">';
							html += result.data.sendTime;
							html += '</time><a href="#" class="span-info" data-value="commentSecondForm">回复</a></p></div></li>';
							var $target = $thisMediaBody.closest('ul.CommentContent1');
							$target.append(html);
							
							$('#commentContent form').remove();
						});
					}
				});
			});
		}
	});
	
	// 删除评论功能
	$('a.adminComment').click(function (event) {
		event.preventDefault();
		
		var $this = $(this);
		var data = {
			newsId: $('.news-header').attr('data-newId'),
			commentId: $this.attr('data-commentId'),
			mainCommentObjectId: $this.attr('data-mainCommentObjectId')
		};
		$.post('/admin/deleteComment', data, function (result) {
			if(result.status == 1) {
				swal({
				  title: "删除评论成功",
				  type: "success"
				}, function () {
					$this.closest('li').remove();
				});
			}
		});
	});
});
